package com.greatwideweb.runhelper;

import java.util.Calendar;
import java.util.GregorianCalendar;


import com.greatwideweb.runhelper.tasks.ISaveRunObserver;
import com.greatwideweb.runhelper.tasks.SaveRunTask;
import com.greatwideweb.util.DateUtil;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;


public class AddScreen extends Activity implements  OnClickListener, ISaveRunObserver{
  
  TextView txtDate;
  Button btnDateChange;
  Button btnSave;
 // NumberPicker milePicker0;
  NumberPicker milePicker1;
  NumberPicker milePicker2;
  NumberPicker milePicker3;
  String errMessage;
  private RunItem selectedRunItem;
  Intent dataSceenIntent;
  
  @Override
  protected void onResume()
  {
    super.onResume();
    try
    {
      Bundle bundle = getIntent().getExtras();
      selectedRunItem = (RunItem)bundle.getSerializable(Application.SELECT_ITEM);
    }catch(Exception e)
    {
      
    }
    if(selectedRunItem != null)
    {
      Log.d("onResume", "selectedRunItem: " + selectedRunItem.getDistancePart());
      milePicker1.setCurrent(selectedRunItem.getDistance());
      int[] seconds = getSeconds(selectedRunItem.getDistancePart());
      if(seconds != null && seconds.length > 0)
      {
        milePicker2.setCurrent(seconds[0]);
        if(seconds.length > 1)
        {
          milePicker3.setCurrent(seconds[1]);
        }
      }
    }
      
  }
  private int[] getSeconds(int distancePart)
  {
    if(distancePart == 0)
      return null;
    int[] result = new int[2];
    String piece = Integer.toString(distancePart);
    result[0] = Integer.parseInt(piece.substring(0, 1));
    if(piece.length() > 1)
      result[1] = Integer.parseInt(piece.substring(1, 2));
    return result;
  }
  GregorianCalendar cal=null;
  @Override
  public void onCreate(Bundle savedInstanceState) 
  {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.add_run);
      txtDate = (TextView) findViewById(R.id.txtDate);
      txtDate.setOnClickListener(this);
      cal = new GregorianCalendar();
      txtDate.setText(formatDate(cal));
      btnSave = (Button)findViewById(R.id.savebtn);
      btnSave.setOnClickListener(this);
      //btnSave.requestFocus();
      // milePicker0 = (NumberPicker) findViewById(R.id.milePicker0);
      milePicker1 = (NumberPicker) findViewById(R.id.milePicker1);
      milePicker2 = (NumberPicker) findViewById(R.id.milePicker2);
      milePicker3 = (NumberPicker) findViewById(R.id.milePicker3);
      dataSceenIntent = new Intent(this,  DataScreen.class);
  }
  private CharSequence formatDate(GregorianCalendar arg) 
  {
    return DateUtil.getMonth(arg.get(Calendar.MONTH)) + " " + arg.get(Calendar.DATE) + " " + arg.get(Calendar.YEAR);
  }
  
  @Override
  public void onClick(View v) 
  {
    // TODO Auto-generated method stub
    if(v.equals(txtDate)) {
      openDateDialog();
    } else if(v.equals(btnSave)) {
      if(isFormValid()) {
        
        SaveRunTask task = new SaveRunTask(this, this);
        task.execute(collectRunDetails());
        
      } 
      
      
    }
    
  }
  private RunItem collectRunDetails() 
  {
    return new RunItem(cal,getDistance(), getDistancePart() );
  }
  private int getDistancePart() 
  {
    return Integer.parseInt(Integer.toString(milePicker2.getCurrent()) + Integer.toString(milePicker3.getCurrent()));
  }
  private int getDistance() 
  {
    return milePicker1.getCurrent();
  }
  private boolean isFormValid() 
  {
    //Log.d("isFormValid", "milePicker0.getCurrent(): " + milePicker0.getCurrent());
    if(milePicker1.getCurrent() + milePicker2.getCurrent()+ milePicker3.getCurrent() == 0) {
      final String nullDistanceError= getString(R.string.validation_error_null_distance);
      Toast.makeText(this, nullDistanceError, Toast.LENGTH_SHORT).show();
      return false;
    }
    return true;
  }
  private void openDateDialog() {
    Log.d("openDateDialog", "enter");
    // TODO Auto-generated method stub
    final Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.date_picker_dialog);
    dialog.setTitle("Change run date");
    final DatePicker datePicker = (DatePicker) dialog.findViewById(R.id.datePicker1);
    btnDateChange = (Button) dialog.findViewById(R.id.btnSaveDate);
    btnDateChange.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        cal = new GregorianCalendar(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth());
        txtDate.setText(formatDate(cal));
        dialog.dismiss();
      }
    });
    dialog.show();
    
    
  }
  @Override
  public void onSaveResult(String message) {
    // TODO Auto-generated method stub
    Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
    startActivityForResult(dataSceenIntent, 0);
  }

  

}
